package com.ruoyi.web.controller.comlbs;


import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.FtpConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.DriverInfo;
import com.ruoyi.system.domain.OrderSendRecord;
import com.ruoyi.system.domain.request.driver.DriverFaceSendParam;
import com.ruoyi.system.domain.request.driver.DriverFaceSendRecordParam;
import com.ruoyi.system.domain.request.driver.DriverInfoSearchParam;
import com.ruoyi.system.service.IDriverInfoService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.StringUtils;

import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 负责人管理Controller
 * 
 * @author wp
 * @date 2023-08-10
 */
@RestController
@RequestMapping("/comlbs/driverInfo")
public class DriverInfoController extends BaseController {
    @Autowired
    private IDriverInfoService driverInfoService;
    @Autowired
    private FtpConfig ftpConfig;

    /**
     * 查询负责人列表
     */
    @ApiOperation("负责人列表")
    @ApiOperationSupport(includeParameters = {"companyId","name","numberPlate","driverTaskStatus"})
    @GetMapping("/list")
    public TableDataInfo list(DriverInfo driverInfo) {
        startPage();
        LoginUser loginUser = getLoginUser();
        if (!loginUser.getUser().isAdmin() && !StringUtils.isNotNull((driverInfo.getCompanyId()))) {
            driverInfo.setCompanyId(loginUser.getUser().getDeptId());
        }
        List<DriverInfo> list = driverInfoService.selectList(driverInfo);
        return getDataTable(list);
    }

    @ApiOperation("负责人列表导出")
    @ApiOperationSupport(includeParameters = {"companyId","name","numberPlate"})
    @Log(title = "负责人管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DriverInfo driverInfo) {
        List<DriverInfo> list = driverInfoService.selectList(driverInfo);
        ExcelUtil<DriverInfo> util = new ExcelUtil<DriverInfo>(DriverInfo.class);
        util.hideColumn("updateTime");
        util.exportExcel(response, list, "负责人管理数据");
    }

    /**
     * 获取负责人详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(driverInfoService.selectOne(id));
    }

    /**
     * 新增负责人
     */
    @Log(title = "负责人管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DriverInfo driverInfo) {
        return toAjax(driverInfoService.save(driverInfo));
    }

    @ApiOperation("编辑负责人")
    @Log(title = "负责人管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DriverInfo driverInfo) {
        return toAjax(driverInfoService.updateDriverInfo(driverInfo));
    }

    @ApiOperation("删除负责人")
    @Log(title = "负责人管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        driverInfoService.deleteDriverInfo(ids);
        return success();
    }

    @ApiOperation("负责人下拉列表")
    @PostMapping("/search")
    public AjaxResult search(@Validated @RequestBody DriverInfoSearchParam param) {
        List<DriverInfo> resultList = driverInfoService.selectList(new DriverInfo()
                .setCompanyId(param.getCompanyId())
                .setName(param.getName()));
        return success(resultList);
    }

    @ApiOperation("FTP文件上传")
    @Log(title = "FTP文件上传", businessType = BusinessType.INSERT)
    @PostMapping("/uploadFile")
    public AjaxResult uploadFile(@RequestPart MultipartFile[] file){
        return success(uploadFileFTP(file, ftpConfig.getDriverPath(), true));
    }

    @ApiOperation("下发人脸照片")
    @Log(title = "下发人脸照片", businessType = BusinessType.UPDATE)
    @PostMapping("/sendFace")
    public AjaxResult sendFace(@Validated @RequestBody DriverFaceSendParam param) {
        driverInfoService.sendFace(param);
        return success();
    }

    @ApiOperation("人脸识别指令下发记录")
    @PostMapping("/driverFaceSendRecord")
    public AjaxResult driverFaceSendRecord(@RequestBody DriverFaceSendRecordParam param) {
        return success(driverInfoService.driverFaceSendRecord(param));
    }
}
